﻿using ExcellentProjectPlus.Aplicacion.Cuestionarios;
using ExcellentProjectPlus.Aplicacion.Cuestionarios.Catalogos;
using ExcellentProjectPlus.Dominio.Cuestionarios.Entidades;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Web.Http.Description;

namespace Servicios.Cuestionarios.Controllers
{
    public class CatalogosController : ApiController
    {
        #region Propiedades
        IServicioDeGestionDeCatalogos _servicioDeGestionDeCatalogos;
        #endregion

        #region Constructores
        public CatalogosController(IServicioDeGestionDeCatalogos servicioDeGestionDeCatalogos)
        {
            _servicioDeGestionDeCatalogos = servicioDeGestionDeCatalogos;
        }
        #endregion

        // GET api/Catalogos
        public IEnumerable<Catalogo> GetCatalogos()
        {
            return _servicioDeGestionDeCatalogos.ObtenerCatalogos();
        }

        // GET api/Catalogos/5
        [ResponseType(typeof(Catalogo))]
        public IHttpActionResult GetCatalogo(int id)
        {
            Catalogo catalogo = _servicioDeGestionDeCatalogos.ObtenerCatalogoPorId(id);
            if (catalogo == null)
            {
                return NotFound();
            }

            return Ok(catalogo);
        }

        // PUT api/Catalogos/5
        public IHttpActionResult PutCatalogo(int id, Catalogo catalogo)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            if (id != catalogo.IdCatalogo)
            {
                return BadRequest();
            }

            _servicioDeGestionDeCatalogos.ModificarCatalogo(catalogo);

            return StatusCode(HttpStatusCode.NoContent);
        }

        // POST api/Catalogos
        [ResponseType(typeof(Catalogo))]
        public IHttpActionResult PostCatalogo(Catalogo catalogo)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            _servicioDeGestionDeCatalogos.AgregarCatalogo(catalogo);

            return CreatedAtRoute("DefaultApi", new { id = catalogo.IdCatalogo }, catalogo);
        }

        // DELETE api/Catalogos/5
        [ResponseType(typeof(Catalogo))]
        public IHttpActionResult DeleteCatalogo(int id)
        {
            Catalogo catalogo = _servicioDeGestionDeCatalogos.ObtenerCatalogoPorId(id);
            if (catalogo == null)
            {
                return NotFound();
            }

            _servicioDeGestionDeCatalogos.EliminarCatalogo(catalogo);

            return Ok(catalogo);
        }

        private bool CatalogoExists(int id)
        {
            return _servicioDeGestionDeCatalogos.ObtenerCatalogoPorId(id) != null;
        }


        //[HttpGet()]
        //[Route("api/catalogos/GetPagedCatalogos/{pageIndex}/{pageSize}")]
        //public IEnumerable<Catalogo> GetPagedCatalogos(int pageIndex, int pageSize)
        //{
        //    return (_servicioDeGestionDeCuestionarios.Catalogos.OrderBy(c => c.IdCatalogo).Skip(pageIndex * pageSize).Take(pageSize)).ToList();
        //}


    }
}